package com.woniu.adapter;

import com.woniu.outlet.dao.*;
import com.woniu.outlet.po.*;
import com.woniu.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@Transactional
public class CompanyAdapter {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyExtendMapper companyExtendMapper;

    @Autowired
    private AssociatesMapper associatesMapper;

    @Autowired
    private CompanyKeyPersonMapper companyKeyPersonMapper;
    
    @Autowired
    private PotentialCustomersMapper potentialCustomersMapper;

    @Autowired
    private PotentialMessageMapper potentialMessageMapper;

    //添加正式客户的信息
    public int addregularcustomer(Company company){
        //添加company表格
        int insert = companyMapper.insert(company);
        Integer companyid = company.getCompanyId();

        //添加companyextend表格
        CompanyExtend companyExtend = company.getCompanyExtend();
        companyExtend.setCompanyId(companyid);
        int insert1 = companyExtendMapper.insert(companyExtend);

        //添加关联公司信息
        Associates associates = company.getAssociates();
        associates.setCompanyId(companyid);
        int insert2 = associatesMapper.insert(associates);

        //添加一个关键人的信息
        CompanyKeyPerson companyKeyPerson = company.getCompanyKeyPerson();
        companyKeyPerson.setCompanyId(companyid);
        int insert3 = companyKeyPersonMapper.insert(companyKeyPerson);
        
        //如果是潜在的客户，那么就删除这个潜在的客户
        Integer pcId = company.getPcId();
        if(pcId!=null){
            //删除潜在客户
            int i = potentialCustomersMapper.deleteByPrimaryKey(pcId);
        }

        //创建一个message对象封装数据到对象当中
        PotentialMessage potentialMessage = new PotentialMessage();
        potentialMessage.setPotentialmessageExchange("potentialcustomer_exchange");
        potentialMessage.setPotentialmessageKey("potentialcustomer.xxx");
        potentialMessage.setPotentialmessageCount(10);
        potentialMessage.setPotentialmessageContext(""+companyid+","+"addregularcustomer");
        potentialMessage.setPotentialmessageStatus(0);
        //将这个message存到数据库
        potentialMessageMapper.insert(potentialMessage);

        return 0;
    }


    //修改企业客户表（主表：company）的具体信息
    public int updatecompany(Company company){
        int i = companyMapper.updateByPrimaryKeySelective(company);
        return i;
    }

    //修改企业的扩展表companyextend
    public int updatecompanyextend(CompanyExtend companyExtend){

        int i = companyExtendMapper.updateByPrimaryKeySelective(companyExtend);
        return i;
    }

    //修改联系人的信息
    public int updatecompanykeyperson(CompanyKeyPerson companyKeyPerson){
        int i = companyKeyPersonMapper.updateByPrimaryKeySelective(companyKeyPerson);
        return i;
    }

    //修改关联企业的信息
    public int updateassociate(Associates associates){
        int i = associatesMapper.updateByPrimaryKeySelective(associates);
        return i;
    }

    //添加关联企业的信息，一般而言一个企业的关联企业是一对多的关系
    public int addassociate(Associates associates){
        int i = associatesMapper.insertSelective(associates);
        return i;
    }

}
